package DAO.Impl;

import java.sql.Timestamp;
import java.util.*;

import org.hibernate.Query;
import org.hibernate.Session;

import common.PagingObject;

import DAO.Interfaces.IStoreDAO;
import POJO.Personnel;
import POJO.Store;

public class StoreDAOImpl extends GenericDAOImpl<Store> implements IStoreDAO{

	public List<Store> selectStore(){
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Store");
		List<Store> result = query.list();
		session.close();
		return result;
	}

	@Override
	public Store selectStore(String sID) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Store where SID = :SID");
		query.setString("SID", sID);
		Store data = (Store) query.uniqueResult(); 
		session.close();
		return data;
	}

	@Override
	public void updateStore(Store store) {
		Session session = sessionFactory.openSession();
		ts = session.beginTransaction();
		
		Query query = session.createQuery("update Store set SADDRESS=:SADDRESS, SLAUNCH=:SLAUNCH, SNAME=:SNAME, STEL=:STEL where SID=:SID");
		query.setInteger("SID", store.getSID());
		query.setString("SADDRESS", store.getSADDRESS());
		query.setDate("SLAUNCH", store.getSLAUNCH());
		query.setString("SNAME", store.getSNAME());
		query.setString("STEL", store.getSTEL());
		query.executeUpdate();
		
		ts.commit();
		session.close();
	}

	@Override
	public int findStoreCount(Store store) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Store where SNAME like :SNAME");
		query.setString("SNAME", "%" + store.getSNAME() + "%");
		int count = query.list().size(); 
		session.close();
		return count;
	}
	
	
	@Override
	public List<Store> selectStore(Store store,PagingObject object) {
		Session session = sessionFactory.openSession();
		Query query = session.createQuery("from Store where SNAME like :SNAME");
		query.setString("SNAME", "%" + store.getSNAME() + "%");
		
		query.setFirstResult(object.getBeginIndex());
		query.setMaxResults(object.getNumOfPerPage());
		List data = query.list();
		session.close();
		return data; 
	}


}
